package com.bw.kotlindemo.kotlin.view.fragment

import android.util.Log
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.bw.kotlindemo.R
import com.bw.kotlindemo.databinding.FragmentLiveListBinding
import com.bw.kotlindemo.java.adapter.LiveListAdapter
import com.bw.kotlindemo.java.model.bean.LiveListBean
import com.bw.kotlindemo.kotlin.base.KBaseFragment
import com.bw.kotlindemo.kotlin.bean.KResultBean
import com.bw.kotlindemo.kotlin.kvm.KLiveVM
import com.bw.kotlindemo.kotlin.view.adapter.KLiveListAdapter

/**
 * desc:直播列表
 * create by xushaodong on 2023/4/6
 */
class KLiveListFragment(_typeResultBean: KResultBean) :
    KBaseFragment<KLiveVM, FragmentLiveListBinding>() {
    val typeResultBean: KResultBean = _typeResultBean
    val page = 1
    var size = 10
    var liveType = 0
    lateinit var adapter: KLiveListAdapter
    var data: MutableList<LiveListBean.ResultBean> = mutableListOf()

    override fun initData() {
        liveType = typeResultBean.id
        setView()
        initVM()
        loadData()
    }

    private fun loadData() {
        mViewModel.getLiveList(liveType, page, size)
    }

    private fun initVM() {
        mViewModel.liveListVM.observe(this) {
            if (page == 1) {
                data.clear()
            }
            data.addAll(it.result)
            adapter.setData(data)
            Log.e("直播","size: "+data.size)
        }
    }

    private fun setView() {
        val gridLayoutManager = GridLayoutManager(activity, 2)
        mDataBinding.rvLiveList.layoutManager = gridLayoutManager
        adapter = KLiveListAdapter(activity)
        mDataBinding.rvLiveList.adapter = adapter

    }

    override fun initLayout(): Int {
        return R.layout.fragment_live_list
    }

    override fun initViewModel(): KLiveVM {
        return ViewModelProviders.of(this).get(KLiveVM::class.java)
    }


}